package com.auto.learning.tools;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.auto.learning.R;

public class ViewImagePagerUtil {

	private Context mContext;
	private ArrayList<View> mPageViews;// 视图列表
	private ViewPager mViewPager;
	private ViewGroup mGroup;
	private int currentPage = 0;// 当前的页面
	private int mPosstion = 0;// 位置
	
	private Handler handler;
	private boolean isCycle;
	private int time = 3000; 
	

	public ViewImagePagerUtil(Context ctx, ArrayList<View> pageViews, ViewPager viewPager, ViewGroup group,Handler handler) {
		this.mContext = ctx;
		this.mPageViews = pageViews;
		this.mViewPager = viewPager;
		this.mGroup = group;
		this.handler = handler;
	};
	
	
	public boolean isCycle() {
		return isCycle;
	}
	
	public void setCycle(boolean isCycle) {
		this.isCycle = isCycle;
		handler.postDelayed(runnable, time);
	}
	
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			handler.postDelayed(this, time);
			if (isCycle) {
				int count =  mPageViews.size();
				if (count > 1) {  
				    int index = mViewPager.getCurrentItem();  
				    index = (index + 1) % count;  
				    mViewPager.setCurrentItem(index, true);  
				}
			}
		}
	};
	

	/**
	 * 建立左右滑动且带指示的元素
	 * 
	 * @param ctx
	 * @param pageViews
	 * @param viewPager
	 * @param group
	 */
	public void buildSlideView() {
		ImageView imageView = null;
		mViewPager.setAdapter(new MyPagerAdapter(mContext, mPageViews));
		if (mPageViews.size() > 1) {
			ImageView[] imageViews = new ImageView[mPageViews.size()];
			Drawable imageDrawable = mContext.getResources().getDrawable(R.drawable.pagectr_active);
			int width = imageDrawable.getMinimumWidth() / 2;
			int height = imageDrawable.getMinimumHeight() / 2;
			for (int i = 0; i < imageViews.length; i++) {
				imageView = new ImageView(mContext);
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
				lp.leftMargin = 5;
				lp.rightMargin = 5;
				imageView.setLayoutParams(lp);
				// imageView.setPadding(width, 0, width, 0);
				imageViews[i] = imageView;
				if (i == 0) {
					imageViews[i].setBackgroundResource(R.drawable.page_unselect);
				} else {
					imageViews[i].setBackgroundResource(R.drawable.page_select);
				}
				mGroup.addView(imageViews[i]);
			}
			mViewPager.setOnPageChangeListener(new GuidePageChangeListener(imageViews));
			mViewPager.setCurrentItem(imageViews.length);
		}

	}

	public class MyPagerAdapter extends PagerAdapter {
		public ArrayList<View> pageViews;
		Context context;
		int mCount;

		public MyPagerAdapter(Context context, ArrayList<View> pageViews) {
			this.pageViews = pageViews;
			this.context = context;
			mCount = pageViews.size();
		}

		@Override
		public void destroyItem(View collection, int position, Object arg2) {
			if (position >= pageViews.size()) {
				int newPosition = position % pageViews.size();
				position = newPosition;
				// ((ViewPager) collection).removeView(views.get(position));
			}
			if (position < 0) {
				position = -position;
				// ((ViewPager) collection).removeView(views.get(position));
			}
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mCount ;// 此处+1才能向右连续滚动
		}

		@Override
		public Object instantiateItem(View collection, int position) {
			if (position >= pageViews.size()) {
				int newPosition = position % pageViews.size();
				position = newPosition;
				mCount++;
			}
			if (position < 0) {
				position = -position;
				mCount--;
			}
			try {
				((ViewPager) collection).addView(pageViews.get(position), 0);
			} catch (Exception e) {
			}
			return pageViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == (object);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			
		}
		
	}

	private class GuidePageChangeListener implements OnPageChangeListener {

		private ImageView[] imageViews;

		public GuidePageChangeListener(ImageView[] imageViews) {
			this.imageViews = imageViews;
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

		}

		public void onPageSelected(int position) {

			currentPage = position;
			Log.w("position", "position = " + position);
			position = position % mPageViews.size();
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[position].setBackgroundResource(R.drawable.page_unselect);
				if (position != i) {
					imageViews[i].setBackgroundResource(R.drawable.page_select);
				}
			}
		}
	}

	/**
	 * @MethodName: getCurrentPage
	 * @Description: TODO 获取当前的显示页 All rights Reserved, Designed By isoftStone
	 *               Copyright: Copyright(C) 2013 Company: isoftStone Holdings
	 *               Limited
	 * @param: @return
	 * @author: JFYU
	 * @return int
	 * @version: V1.0
	 * @date 2013-11-4 下午3:59:30
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int position) {
		mPosstion = position;
	}

	public void toPage1() {
		mViewPager.setCurrentItem(0);
	}
}
